package com.wandousoushu.jiusen.ui.my

import android.app.Activity
import android.content.Intent
import android.os.AsyncTask.execute
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wandousoushu.jiusen.MyApplication
import com.wandousoushu.jiusen.R
import com.wandousoushu.jiusen.adapter.BookMarkAdapter
import com.wandousoushu.jiusen.adapter.BookShelfAdapter
import com.wandousoushu.jiusen.base.VMBaseActivity
import com.wandousoushu.jiusen.booksource.bean.BookBean
import com.wandousoushu.jiusen.booksource.data.entities.Book
import com.wandousoushu.jiusen.booksource.data.entities.ReadBook
import com.wandousoushu.jiusen.booksource.model.BookManager
import com.wandousoushu.jiusen.lib.dialogs.alert
import com.wandousoushu.jiusen.lib.dialogs.noButton
import com.wandousoushu.jiusen.lib.dialogs.okButton
import com.wandousoushu.jiusen.ui.book.read.Help
import com.wandousoushu.jiusen.ui.book.read.ReadBookViewModel
import com.wandousoushu.jiusen.ui.info.BookInfoActivity
import com.wandousoushu.jiusen.ui.search.SearchActivity
import com.wandousoushu.jiusen.util.*
import com.wandousoushu.jiusen.web.http.MainHttpUtils
import kotlinx.android.synthetic.main.activity_bookmark.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.json.JSONArray
import org.json.JSONObject
import java.util.*

class BookMarkActivity : AppCompatActivity() {

    var adapter: BookMarkAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        //Help.setOrientation(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bookmark)
        bookmark_ll_back.setOnClickListener(View.OnClickListener { finish() })
        bookmark_add!!.onClick {
            val intent = Intent(applicationContext, SearchActivity::class.java)
            startActivity(intent)
        }
        InitData()
    }


    private fun InitData() {
        //纵向线性布局
        val layoutManager = GridLayoutManager(applicationContext, 3)
        bookmark_recycerview!!.layoutManager = layoutManager
    }

    override fun onResume() {
        super.onResume()
        mHandle.sendEmptyMessage(0)
    }

    private fun AddBookShlef(book: Book) {
        Thread(Runnable {
            BookManager.GetInstance().AddBooktoShelf(book, 2)
        }).start()

    }

    private val mHandle: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                0 -> {
                    var bookList = BookManager.GetInstance().GetBookListByType(1)
                    if (bookList.size == 0) {
                        bookmark_recycerview.gone()
                        bookmark_ll_none.visible()
                    } else {
                        bookmark_ll_none.gone()
                        bookmark_recycerview.visible()
                    }
                    adapter = BookMarkAdapter(applicationContext, bookList)
                    adapter!!.setActionListener { book ->
                        if (!BookManager.GetInstance().IsInBookShelf(book)) {
                            alert(title = getString(R.string.add_to_shelf)) {
                                message = getString(R.string.check_add_bookshelf, book.name)
                                okButton {
                                    Thread(Runnable {
                                        AddBookShlef(book)
                                        sendEmptyMessage(4)
                                    }).start()
                                }
                                noButton {

                                }
                            }.show().applyTint()
                        } else {
                            sendEmptyMessage(3)
                        }
//                        startActivity<BookInfoActivity>(
//                                Pair("name", book.name),
//                                Pair("author", book.author),
//                        )
                    }
                    adapter!!.setLongClickListener { book ->
                        alert(title = getString(R.string.sure_del_bookmark)) {
                            message = getString(R.string.check_del_bookmark,book.name)
                            okButton {
                                Thread(Runnable {
                                    BookManager.GetInstance().RemoveBookMark(book, 1)
                                    sendEmptyMessage(5)
                                    sendEmptyMessage(0)
                                }).start()
                            }
                            noButton {

                            }
                        }.show().applyTint()
                    }
                    bookmark_recycerview!!.adapter = adapter
                }
                1 -> Toast.makeText(applicationContext, resources.getString(R.string.bookmark_http_fail), Toast.LENGTH_SHORT).show()
                2 -> Toast.makeText(applicationContext, msg.obj.toString(), Toast.LENGTH_SHORT).show()
                3 -> toast(R.string.have_to_shelf)
                4 -> toast(R.string.add_success)
                5 -> toast(R.string.del_success)
            }
        }
    }

}